package com.yb.common.handler;

import java.nio.file.AccessDeniedException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yb.common.domain.AjaxObject;
import com.yb.common.exception.BaseException;

/**
 * 全局异常处理
 */
@ControllerAdvice
public class GlobalExceptionHandler {
	
	Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
	
	@ResponseBody 
	@ExceptionHandler(BaseException.class)
	public AjaxObject handleException(BaseException e) {
		logger.error(e.getMessage(), e);
		return AjaxObject.newError();
	}
	
	@ResponseBody 
	@ExceptionHandler(Exception.class)
	public AjaxObject handleException(Exception e) {
		logger.error(e.getMessage(), e);
		return AjaxObject.newError();
	}
	
	@ResponseBody
	@ExceptionHandler(AccessDeniedException.class)
	public AjaxObject handleException(AccessDeniedException e) {
		logger.error(e.getMessage(), e);
		return AjaxObject.newError();
	}
	
	@ResponseBody
	@ExceptionHandler(BadCredentialsException.class)
	public AjaxObject handleException(BadCredentialsException e) {
		logger.error(e.getMessage(), e);
		return AjaxObject.newError();
	}
	
	@ResponseBody
	@ExceptionHandler(InternalAuthenticationServiceException.class)
	public AjaxObject handleException(InternalAuthenticationServiceException e) {
		logger.error(e.getMessage(), e);
		return AjaxObject.newError();
	}
}
